<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>发送数据到服务器</button>
    <script>
      // 客户端（浏览器）websocket
      const ws = new WebSocket("ws://localhost:5008"); // 创建一个websocket，同时，发送连接到服务器
      ws.onopen = function () {
        // http握手完成
        console.log("连接已建立");
      };

      ws.onmessage = function (e) {
        console.log("来自服务器的数据", e.data);
      };

      ws.onclose = function () {
        console.log("通道关闭");
      };

      document.querySelector("button").onclick = function () {
        ws.send("123");
      };
      //   ws.close(); //客户端主动断开连接
    </script>
  </body>
</html>

